/**
 * 
 */
package com.example.android_is2.ListenerMap;

import java.util.List;

import android.location.Location;
import android.util.Log;

import com.example.android_is2.InfoGeoPoints;
import com.example.android_is2.MainActivity;
import com.example.android_is2.MapItemizedOverlay;
import com.example.android_is2.MapRouteActivity;
import com.example.android_is2.db.DBInfo;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

/**
 * @author KAIN-PC
 *
 */
public class LocationListenerMapPathDraw extends LocationListenerMap{

  public static final String MAP_HERE = "I'm HERE";
  

  public LocationListenerMapPathDraw(MapView mapView, List<Overlay> overlays,
      MapItemizedOverlay itemizedoverlay_my, MapItemizedOverlay itemizedoverlay, DBInfo db) {
    super(mapView, overlays, itemizedoverlay_my, itemizedoverlay, db);
    // TODO Auto-generated constructor stub
  }

  @Override
  public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    // Aggiorna il marker della mappa
    mapView.invalidate();
    // Aggiorna la location
    if (location != null) {
      int geoLat = (int) (location.getLatitude() * 1E6);
      int geoLng = (int) (location.getLongitude() * 1E6);

      Log.i(MainActivity.TAG + getClass(), "Lat: " + geoLat + " - Lng: " + geoLng);
      GeoPoint point = new GeoPoint(geoLat, geoLng);
      if (point != null) {
        // Rimozione Overlay precedenti per aggiornamento
        getItemizedoverlay_my().clearOverlay();

        getMapcontroller().animateTo(point);
        OverlayItem overlayitem = new OverlayItem(point, MAP_HERE, "Lat: "
            + Double.toString(getRound(location.getLatitude(), 5)) + "\nLng: "
            + Double.toString(getRound(location.getLongitude(), 5)));

        getItemizedoverlay_my().addOverlay(overlayitem);
        getOverlays().add(getItemizedoverlay_my());

        Log.i(MainActivity.TAG + getClass(),
            "Lat: " + (Double.toString(getRound(location.getLatitude(), 5))));
        Log.i(MainActivity.TAG + getClass(),
            "Long: " + (Double.toString(getRound(location.getLongitude(), 5))));
        Log.i(MainActivity.TAG + getClass(),
            "Spd: " + (Double.toString(getRound(location.getSpeed() * 3.6, 1)) + " km/h"));
        Log.i(MainActivity.TAG + getClass(),
            "Alt: " + (Integer.toString((int) location.getAltitude()) + " m"));
        Log.i(MainActivity.TAG + getClass(),
            "Acc: " + (Integer.toString((int) location.getAccuracy()) + " m"));
      }
    }
    getOverlays().remove(getItemizedoverlay());
    getItemizedoverlay().clearOverlay();
    getDbInfo().open();
    InfoGeoPoints IGP[] = getDbInfo().TakeInfo(location.getLatitude(), location.getLongitude());

    if (IGP.length == 0)
      Log.i(MainActivity.TAG + getClass(), "No Match \n");
    else {
      GeoPoint point = null;
      OverlayItem overlayitem = null;
      for (int k = 0; k < IGP.length; k++) {
        // Log.i(MainActivity.TAG+getLocalClassName(),"ExactMatch : Text"+IGP[k].getText()+" lat :"+IGP[k].getLat()+" lon : "+IGP[k].getLong()+"\n");
        // //estrazione dei dati
        point = new GeoPoint((int) (IGP[k].getLat() * 1E6), (int) (IGP[k].getLong() * 1E6));
        overlayitem = new OverlayItem(point, IGP[k].getText(), "Lat: "
            + Double.toString(LocationListenerMap.getRound(point.getLatitudeE6(), 5)) + "\nLng: "
            + Double.toString(LocationListenerMap.getRound(point.getLongitudeE6(), 5)));

        getItemizedoverlay().addOverlay(overlayitem);
      }
    }
    if (this.getItemizedoverlay() != null && MapRouteActivity.isPressedBtn1 == true) {
      getOverlays().add(getItemizedoverlay());
    }
    getDbInfo().close();
  }

}
